package com.atguigu.gmall.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: 封捷
 * @create-date: 2022/9/13 9:48
 */
@Configuration
public class DelayMessageConfig {

    // 声明常量：交换机名称
    public static final String EXCHANGE_DELAY = "exchange.delay.220309";

    // 声明常量：路由键名称
    public static final String ROUTING_KEY_DELAY = "routing.key.delay.220309";

    // 声明常量：队列名称
    public static final String QUEUE_DELAY = "queue.delay.220309";

    @Bean
    public CustomExchange getDelayExchange() {

        // 声明交换机的类型
        String type = "x-delayed-message";

        // 封装交换机所需参数
        Map<String, Object> argumentsMap = new HashMap<>();
        argumentsMap.put("x-delayed-type", "direct");

        // 创建交换机对象并返回
        return new CustomExchange(EXCHANGE_DELAY, type, true, false, argumentsMap);
    }

    @Bean
    public Queue getDelayQueue() {
        return new Queue(QUEUE_DELAY, true, false, false);
    }

    @Bean
    public Binding getBiding() {
        return BindingBuilder
                .bind(getDelayQueue())
                .to(getDelayExchange())
                .with(ROUTING_KEY_DELAY)
                .noargs();
    }
}
